/**
 * 
 */
package com.swim.traininfo.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.swim.traininfo.entity.TrainInfo;
import com.swim.traininfo.dao.TrainInfoDao;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 培训资讯Service
 * @author 刘军武
 * @version 2021-04-25
 */
@Service
@Transactional(readOnly=true)
public class TrainInfoService extends CrudService<TrainInfoDao, TrainInfo> {
	
	@Value("${server.context-path}")
	private String contextPath;
	
	/**
	 * 获取单条数据
	 * @param trainInfo
	 * @return
	 */
	@Override
	public TrainInfo get(TrainInfo trainInfo) {
		return super.get(trainInfo);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param trainInfo
	 * @return
	 */
	@Override
	public Page<TrainInfo> findPage(Page<TrainInfo> page, TrainInfo trainInfo) {
		Page<TrainInfo> pageList = super.findPage(page, trainInfo);
		List<TrainInfo> trainInfoList = pageList.getList();
		if (!CollectionUtils.isEmpty(trainInfoList)) {
			for (TrainInfo trainInfoOne : trainInfoList) {
				//获取培训资讯图片
				List<FileUpload> fileUploadListOne = FileUploadUtils.findFileUpload(trainInfoOne.getId(), "trainInfo_image");
				if (!CollectionUtils.isEmpty(fileUploadListOne)) {
					List<String> trainInfoImageUrlList = new ArrayList<String>();	// 培训资讯图片url集合
					trainInfoImageUrlList.add(contextPath + "/userfiles/fileupload/" + fileUploadListOne.get(0).getFileEntity().getFilePath() + fileUploadListOne.get(0).getFileEntity().getFileId() + "." + fileUploadListOne.get(0).getFileEntity().getFileExtension());
					trainInfoOne.setTrainInfoImageUrlList(trainInfoImageUrlList);
				}
			}
		}
		pageList.setList(trainInfoList);
		return pageList;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param trainInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(TrainInfo trainInfo) {
		super.save(trainInfo);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(trainInfo.getId(), "trainInfo_image");
	}
	
	/**
	 * 更新状态
	 * @param trainInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(TrainInfo trainInfo) {
		super.updateStatus(trainInfo);
	}
	
	/**
	 * 删除数据
	 * @param trainInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(TrainInfo trainInfo) {
		super.delete(trainInfo);
	}
	
}